<template>
    <!-- 文化活动 -->
    <base-breadcrumb :title="$route.meta.title">
        <!-- 搜索 -->
        <searchData :searchData="searchArr" @search="search"></searchData>
        <!-- 表格 -->
        <a-card :bordered="false">
            <!-- <title-name :title="$route.meta.title"></title-name> -->
            <a-button
                class="mb12"
                icon="plus"
                type="primary"
                @click="handleManage(2)"
                v-btnPermission="'lu_0014_add_01'"
                >新增</a-button
            >
            <!-- 导入 -->
            <ExcelImportButton
                ref="excelImportButton"
                icon="import"
                class="ml12"
                @handleSubmit="handleSubmit"
                v-btnPermission="'lu_0014_imp_05'"
            />
            <!-- 导出 -->
            <excel
                v-btnPermission="'lu_0014_exp_06'"
                class="ml12"
                ref="exportAll"
                fileName="职工书屋"
                is-click-generate="false"
            >
                <a-button type="primary" icon="download" :loading="loading" slot="excelBtn" @click="exportAll"
                    >导出</a-button
                >
            </excel>
            <export-mould
                v-btnPermission="'lu_0014_imp_05'"
                style="margin-left: 5px"
                templateCode="职工书屋模板.xlsx"
                icon="cloud-download"
            ></export-mould>
            <!-- 表格 -->
            <list-table
                ref="table"
                tableLayout="fixed"
                size="default"
                rowKey="id"
                :columns="columns"
                :request="loadData"
                :alert="true"
                show-size-changer
                show-quick-jumper
                showPagination="auto"
            >
                <template slot="operation" slot-scope="text, record">
                    <MoreButton>
                    <a @click="handleManage(0, record)" v-btnPermission="'lu_0014_loo_04'">查看</a>

                    <a @click="handleManage(1, record)" v-btnPermission="'lu_0014_edi_02'">编辑</a>
                    <!-- 删除 -->
                    <a class="del_btn" @click="handleRemove(record.id)" v-btnPermission="'lu_0014_del_03'">删除</a>
                    </MoreButton>
                </template>
            </list-table>
        </a-card>
    </base-breadcrumb>
</template>

<script>
import { STable } from '@/components' // 表格
import { getPageUnionBookHouse, deleteUnionBookHouse, addUnionBookHouseImport } from '@/api/union/culturalActivities' // api
import Selector from '@/components/Select/Selector'
import BasePage from '@/views/basePage' // 基础配置
import ExportMould from '@/components/ExportMould'
const searchArr = [
    // 搜索框
    {
        name: '书屋名称',
        type: 'input',
        value: 'bookHouseName',
    },
    {
        name: '书屋等级',
        type: Selector,
        valueKey: 'bookLevel',
        selectType: '6',
        urlType: 'union',
    },
    {
        name: '建设时间',
        type: 'dateRange',
        value: 'buildDate',
        attr: {
            rangeFormat: ['buildDateStart', 'buildDateStartEnd'],
        },
    },
    {
        name: '命名挂牌时间',
        type: 'dateRange',
        value: 'callNameDate',
        attr: {
            rangeFormat: ['callNameDateStart', 'callNameDateEnd'],
        },
    },

    {
        name: '管理员',
        type: 'input',
        value: 'adminName',
    },
]
const columns = [
    {
        title: '序号',
        width: 80,
        dataIndex: 'index',
        customRender() {
            return arguments[2] + 1
        },
    },
    {
        title: '书屋名称',
        ellipsis: true,
        width: '15%',
        dataIndex: 'bookHouseName',
    },
    {
        title: '建设时间',
        ellipsis: true,
        dataIndex: 'buildDate',
    },
    {
        title: '书屋等级',
        ellipsis: true,
        dataIndex: 'bookLevelStr',
    },
    {
        title: '命名挂牌时间',
        ellipsis: true,
        dataIndex: 'callNameDate',
    },
    {
        title: '颁发单位',
        dataIndex: 'conferOrg',
        ellipsis: true,
    },
    {
        title: '书屋面积(㎡)',
        dataIndex: 'houseArea',
        ellipsis: true,
    },
    {
        title: '藏书量(册)',
        dataIndex: 'bookNum',
        ellipsis: true,
    },
    {
        title: '管理员',
        dataIndex: 'adminName',
        ellipsis: true,
    },
    {
        title: '联系电话',
        dataIndex: 'phone',
        ellipsis: true,
    },

    {
        title: '操作',
        dataIndex: 'operation',
        width: '180px',
        align: 'center',
        scopedSlots: { customRender: 'operation' },
    },
]
export default {
    name:'a'+Date.now(),
    extends: new BasePage(),
    components: {
        STable,
        ExportMould,
    },
    data() {
        this.columns = columns
        this.searchArr = searchArr
        return {
            queryParam: {}, // 查询参数
            totalSize: 0,
            loading: false,
            loadData: (parameter) => {
                const requestParameters = Object.assign({}, parameter, { query: this.queryParam })
                return getPageUnionBookHouse(requestParameters).then((res) => {
                    console.log(res)
                    this.totalSize = res.totalSize
                    return res
                })
            }, // 加载数据方法 必须为 Promise 对象
            removeApi: { remove: deleteUnionBookHouse }, // api
            importCheck: {
                序号: 'index',
                单位: 'orgIdStr',
                书屋名称: 'bookHouseName',
                建设时间: 'buildDate',
                书屋等级: 'bookLevelStr',
                命名挂牌时间: 'callNameDate',
                颁发单位: 'conferOrg',
                '书屋面积(㎡)': 'houseArea',
                '投入金额(万元)': 'houseMoney',
                '藏书量(册)': 'bookNum',
                管理员: 'adminName',
                管理员联系方式: 'phone',
                地址: 'address',
                书屋硬件设施: 'houseDevice',
                备注: 'remarks',
            }, // 导入字段对应
        }
    },
    mounted() {
        this.$refs.exportAll.tableToExcelFile(this.$refs.table)
    },
    methods: {
        // 搜索
        search(value) {
            value && (this.queryParam = value)
            this.$refs.table.refresh(true)
        },
        // 操作栏
        handleManage(type, record) {
            let query = {
                editModel: type,
            }
            if (record && record.id) {
                query.id = record.id
            }
            this.$router.push({
                name: '/union/culturalActivities/workerHouse/updateWorkHouse',
                query,
            })
        },
        /**
         * 导出
         */
        exportAll() {
            this.loading = true
            let searchData = Object.assign(
                {
                    pageSize: this.totalSize,
                    pageNo: 1,
                },

                this.queryParam
            )
            getPageUnionBookHouse(searchData)
                .then((res) => {
                    res.data.forEach((item, index) => {
                        item.index = index
                    })
                    this.loading = false
                    this.$refs.exportAll.setDataAndGenerate(res.data)

                    this.$message.success('导出成功')
                })
                .catch(() => {
                    this.loading = false
                })
        },
        /**
         * 导入记录
         */
        handleSubmit(e) {
            let form = []
            console.log(e)
            for (let index in e) {
                form.push({})
                let keys = e[index]
                let listItem = form[index]
                // 校验excel的表头数量
                // if (Object.keys(keys)?.length === 14) {
                // 校验excel的表头字段
                for (let i in this.importCheck) {
                    let item = this.importCheck[i]
                    if (!Object.keys(keys).includes(i)) {
                        // this.$message.warning(`${i}缺失`)
                        // return
                    } else {
                        listItem[item] = keys[i]
                    }
                }
                // } else {
                //     this.$message.warning('数据不完整')
                //     return
                // }
            }
            addUnionBookHouseImport(form).then((res) => {
                console.log(res)
                if (res.data.length > 0) {
                    this.$info({
                        title: '提示',
                        content: function () {
                            return res.data.map((i, index) => (
                                <p>
                                    {index + 1}:{i}
                                </p>
                            ))
                        },
                        onOk() {},
                    })
                }
                this.$refs.table.refresh(true)
            })
            console.log(form)
        },
    },
}
</script>
